<template>
  <view class="goods-detail" v-if="goods">
    <!-- 1.1 轮播图 开始 -->
    <u-swiper
      :list="goods.pics"
      name="pics_big"
      height="750"
      @click="handlePreviewImage"
      mode="number"
      :effect3d="true"
    ></u-swiper>
    <!-- 1.1 轮播图 结束 -->

    <!-- 2.1 价格 开始 -->
    <view class="goods-price">￥{{ goods.goods_price }}</view>
    <!-- 2.1 价格 结束 -->

    <!-- 3.1 名称 开始 -->
    <view class="goods-name">{{ goods.goods_name }}</view>
    <!-- 3.1 名称 结束 -->

    <!-- 4.1 图文详情 开始 -->
    <view class="info">
      <view class="info-title"> 图片详情</view>
      <rich-text :nodes="goods.goods_introduce"></rich-text>
      <u-parse :html="goods.goods_introduce"></u-parse>
    </view>
    <!-- 4.1 图文详情 结束 -->

    <!-- 5.1 购物订单栏 开始 -->
    <view class="navigation">
      <view class="left">
        <view class="item">
          <u-icon
            name="server-fill"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">客服</view>
        </view>
        <view class="item">
          <u-icon
            name="home"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">店铺</view>
        </view>
        <view class="item car">
          <u-badge
            class="car-num"
            :count="allCount"
            type="error"
            :offset="[-3, -6]"
          ></u-badge>
          <u-icon
            name="shopping-cart"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <navigator
            url="/pages/cate/cate"
            class="text u-line-1"
            open-type="reLaunch"
            >购物车</navigator
          >
        </view>
      </view>
      <view class="right">
        <view class="cart btn u-line-1" @click="handleInCart">加入购物车</view>
        <view class="buy btn u-line-1">立即购买</view>
      </view>
    </view>
    <!-- 5.1 购物订单栏 结束 -->
  </view>
</template>

<script>
export default {
  data() {
    return {
      goods: null,
    };
  },
  computed: {
    // 购买的总数量
    allCount() {
      return this.$store.getters.allCount;
    },
  },
  async onLoad({ id = 17927 }) {
    // const id = 17927;

    const res = await this.$u.get("/goods/detail", { goods_id: id });
    this.goods = res.message;
  },
  methods: {
    handlePreviewImage(index) {
      const urls = this.goods.pics.map((v) => v.pics_big);
      const current = urls[index];
      uni.previewImage({ urls, current });
    },
    handleInCart() {
      const index = this.$store.state.cartList.findIndex(
        (goods) => goods.goods_id === this.goods.goods_id
      );
      const cartList = this.$store.state.cartList;
      if (index === -1) {
        cartList.push({ ...this.goods, checked: true, count: 1 });
      } else {
        cartList[index].count++;
      }
      this.$store.commit("setCartList", cartList);

      uni.showToast({
        title: "恭喜添加成功",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.goods-detail {
  padding-bottom: 102rpx;
}
.goods-price {
  color: $themeColor;
}
.navigation {
  position: fixed;
  width: 100%;
  left: 0;
  bottom: 0;
  display: flex;
  margin-top: 100rpx;
  border: solid 2rpx #f2f2f2;
  background-color: #ffffff;
  padding: 16rpx 0;
  .left {
    display: flex;
    font-size: 20rpx;
    .item {
      margin: 0 30rpx;
      &.car {
        text-align: center;
        position: relative;
        .car-num {
          position: absolute;
          top: -10rpx;
          right: -10rpx;
        }
      }
    }
  }
  .right {
    display: flex;
    font-size: 28rpx;
    align-items: center;
    .btn {
      line-height: 66rpx;
      padding: 0 30rpx;
      border-radius: 36rpx;
      color: #ffffff;
    }
    .cart {
      background-color: #ed3f14;
      margin-right: 30rpx;
    }
    .buy {
      background-color: #ff7900;
    }
  }
}
</style>
